<?php

/**
 * SmartPHP framework
 *
 * @name SmartPHP framework
 * @package SmartPHP
 * @author 费尔 <admin@smartphp.cn>
 * @version 1.0 Beta
 * @link http://www.smartphp.cn
 * @copyright smartphp.cn
 * @license <a href="http://www.smartphp.cn">smartphp</a>
 */

namespace Base;

/**
 * 加载文件
 * @author 费尔
 */
class Loader {

    /**
     * 初始自动加载
     * @access public
     */
    public static function initAutoload() {
        self::registerAutoload(function ($class) {
            list($name, $class) = explode('\\', $class, 2);
            return self::import(dirname(__DIR__) . DS . $name . DS . str_replace('\\', DS, $class) . '.class.php');
        });
    }

    /**
     * 注册自动加载机制
     * @access public
     * @param string $autoload
     */
    public static function registerAutoload($autoload = '') {
        !empty($autoload) && spl_autoload_register($autoload);
    }

    /**
     * 加载文件
     * @access public
     * @param string $filename
     * @return boolean|mixed
     */
    public static function import($filename, $return = false) {
        if (is_file($filename)) {
            if ($return) {
                return include $filename;
            }
            require_once $filename;
            return true;
        }
        return false;
    }

    /**
     * 实例模型
     * @access public
     * @param string $table
     * @param string $prefix
     * @param string $dsn
     * @param string $user
     * @param string $password
     * @param array $params
     * @param string $prefix
     * @param string $keyquote
     * @return model
     */
    public static function model($table = '', $prefix = '', $dsn = '', $user = '', $password = '', $params = [], $keyquote = '') {
        static $_model = null;
        return !empty($table) && class_exists(($class = '\\Model\\' . Route::parseName($table, 1))) ? ($_model[$class] ?? $_model[$class] = new $class($table, $prefix, $dsn, $user, $password, $params, $keyquote)) : self::db($table, $prefix, $dsn, $user, $password, $params, $keyquote);
    }

    /**
     * 实例数据库操作
     * @access public
     * @param string $table
     * @param string $prefix
     * @param string $dsn
     * @param string $user
     * @param string $password
     * @param array $params
     * @param string $prefix
     * @param string $keyquote
     * @return model
     */
    public static function db($table = '', $prefix = '', $dsn = '', $user = '', $password = '', $params = [], $keyquote = '') {
        return new Model($table, $prefix, $dsn, $user, $password, $params, $keyquote);
    }

    /**
     * 实例视图控制器
     * @staticvar \Base\View $view
     * @return \Base\View
     */
    public static function view() {
        static $_view = null;
        return $_view ?: $_view = new View;
    }

    /**
     * URL重定向
     *
     * @param string $url 跳转的URL表达式
     * @param array $params 其它URL参数
     * @param string $msg 跳转提示信息
     */
    public static function redirect($url) {
        exit(header('Location:' . $url));
    }

}
